﻿using System;
using System.IO;
using System.Linq;
using System.Text;
using MongoDB.Bson;
using MongoDB.Driver;
using MimosaProductsEF;
using System.Globalization;
using MimosaProductsEF.Model;
using System.Collections.Generic;

namespace ProductReport
{
    public class Program
    {
        static void Main()
        {
            var mongoClient = new MongoClient("mongodb://localhost/");
            var mongoServer = mongoClient.GetServer();
            var database = mongoServer.GetDatabase("ProductReport");

            mongoServer.Connect();

            using (MimosaProductsEntitiesEF db = new MimosaProductsEntitiesEF())
            {
                using (mongoServer.RequestStart(database))
                {
                    var collection = database.GetCollection<BsonDocument>("product");

                    var productCollections = from s in db.Sales
                                             join p in db.Products on s.ProductId equals p.ProductId
                                             join v in db.Vendors on p.VendorId equals v.VendorId
                                             group s by s.ProductId into y
                                             select new
                                             {
                                                 ProductId = y.Key,
                                                 ProductName = y.Select(x => x.Product.ProductName).FirstOrDefault(),
                                                 VendorName = y.Select(x => x.Product.Vendor.VendorName).FirstOrDefault(),
                                                 TotalQuantitySold = y.Sum(x => x.Quantity),
                                                 TotalIncomes = y.Sum(x => x.Sum)
                                             };

                    foreach (var prod in productCollections)
                    {
                        BsonDocument product = new BsonDocument()
                            .Add("_Id", prod.ProductId)
                            .Add("Name", prod.ProductName)
                            .Add("VendorName", prod.VendorName)
                            .Add("TotalQuantity", prod.TotalQuantitySold)
                            .Add("TotalIncomes", prod.TotalIncomes);

                        collection.Insert(product);

                        StringBuilder jsonSb = new StringBuilder();
                        jsonSb.AppendLine("{");
                        jsonSb.AppendLine(string.Format("\t\"product-id\" : {0},", prod.ProductId));
                        jsonSb.AppendLine(string.Format("\t\"product-name\" : \"{0}\",", prod.ProductName));
                        jsonSb.AppendLine(string.Format("\t\"vendor-name\" : \"{0}\",", prod.VendorName));
                        jsonSb.AppendLine(string.Format("\t\"total-quantity-sold\" : {0},", prod.TotalQuantitySold));
                        jsonSb.AppendLine(string.Format("\t\"total-incomes\" : {0},", prod.TotalIncomes.ToString("F2", CultureInfo.CreateSpecificCulture("en-US"))));
                        jsonSb.AppendLine("}");

                        using (StreamWriter file = new StreamWriter(string.Format("../../Product-Reports/{0}.json", prod.ProductId), false))
                        {
                            file.WriteLine(jsonSb.ToString());
                        }
                    }
                }
            }

            mongoServer.Disconnect();
        }
    }
}
